package com.swsc.xapp.model;

import java.io.Serializable;

/**
 * @Author chencl
 * @Date 2021/04/28 22:39
 * @Version: 1.0
 * @Description 统一返回
 */
public class RestResp<T> implements Serializable {
	private static final long serialVersionUID = 1L;

	private static final int CODE_SUCCESS = 200;
	private static final int CODE_FAIL = 500;
	private static final String MSG_SUCCESS = "success";
	private static final String MSG_FAIL = "fail";
	public int code;
	public String message;
	public T data;
	public String path;

	public Long currentTime;

	public RestResp() {
	}

	protected RestResp(int code, String message, String path, T data) {
		this.code = code;
		this.message = message;
		this.path = path;
		this.data = data;
		this.currentTime = System.currentTimeMillis();
	}

	public static <T> RestResp<T> success(T data) {
		return new RestResp(CODE_SUCCESS, MSG_SUCCESS, null, data);
	}

	public static RestResp success() {
		return new RestResp(CODE_SUCCESS, MSG_SUCCESS, null, null);
	}

	public static <T> RestResp<T> success(String message, T data) {
		return new RestResp(CODE_SUCCESS, message, null, data);
	}

	public static RestResp fail() {
		return fail(CODE_FAIL, MSG_FAIL);
	}

	public static RestResp fail(String message) {
		return fail(CODE_FAIL, message);
	}

	public static RestResp fail(int code, String message){
		return fail(code, message, null);
	}

	public static RestResp fail(int code, String message, String path){
		return new RestResp(code, message, path, null);
	}

	public static RestResp error() {
		return error(CODE_FAIL, "Internal Server Error");
	}

	public static RestResp error(String message) {
		return error(CODE_FAIL, message);
	}

	public static RestResp error(int code, String message) {
		return new RestResp(code, message, null, null);
	}

	public static void throwException(String message){
		throw new RuntimeException(message);
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public Long getCurrentTime() {
		return currentTime;
	}

	public void setCurrentTime(Long currentTime) {
		this.currentTime = currentTime;
	}

}